#include"createIndex.c"
#include"Python.h"
#include<math.h>

static PyObject* put(PyObject* self, PyObject* args)
{
    int word,article,position;
 
    if (!PyArg_ParseTuple(args, "iii", &word,&article,&position))
        return NULL;
 
    return Py_BuildValue("i", _indeks_put(word,article,position));
}

static PyObject* mock_put(PyObject* self, PyObject* args)
{
    int word,article,position;
 
    if (!PyArg_ParseTuple(args, "iii", &word,&article,&position))
        return NULL;
 
    return Py_BuildValue("i", _mock_put(word,article,position));
}

static PyObject* start_structure(PyObject* self, PyObject* args)
{
 
    return Py_BuildValue("i", _start_structure() );
}

static PyObject* indeks_start(PyObject* self, PyObject* args)
{
    _indeks_start();
    return Py_BuildValue("i", 0) ;
}


static PyObject* indeks_serialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _indeks_serialize(fileName);
    return Py_BuildValue("i", 0) ;
}

static PyObject* indeks_deserialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _indeks_deserialize(fileName);
    return Py_BuildValue("i", 0) ;
}

static PyObject* fully_indeks_serialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _fully_indeks_serialize(fileName);
    return Py_BuildValue("i", 0) ;
}

static PyObject* fully_indeks_deserialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    serializeFile = fopen(fileName,"rb");
    //_fully_indeks_deserialize(fileName);
    page_initialize();
    return Py_BuildValue("i", 0) ;
}


static PyObject* getArticles(PyObject* self, PyObject* args)
{
    int word;
    if (!PyArg_ParseTuple(args, "i", &word))
        return NULL;
    if( word >= number_of_words )
        return NULL;   
    page_deserialize(word);
    printf("%d %f \n",vec_article[word].len, log(vec_article[word].len + 2.) );
    PyObject* lista = PyList_New( vec_article[word].len);
    int i;
    for( i = 0 ; i < vec_article[word].len; i++)
    {
        PyList_SetItem(lista, (Py_ssize_t) i, PyLong_FromLong( vec_article[word].table[i]) );
    }
    return lista ;
}



static PyObject* getFloats(PyObject* self, PyObject* args)
{
    int word;
    if (!PyArg_ParseTuple(args, "i", &word))
        return NULL;
    if( word >= number_of_words )
        return NULL  ;  
    page_deserialize(word);
    PyObject* lista = PyList_New( vec_article[word].len);
    int i;
    for( i = 0 ; i < vec_article[word].len; i++)
    {
        PyList_SetItem(lista, (Py_ssize_t) i, PyFloat_FromDouble( (double) vec_float[word].table[i]) );
    }
    return lista ;
}

 
static PyObject* extern_getVal(PyObject* self, PyObject* args)
{
    char* word;
    if (!PyArg_ParseTuple(args, "s", &word))
        return NULL;
   return Py_BuildValue("i",  getVal(word) );
}

static PyObject* indexGet(PyObject* self, PyObject* args)
{
    int word;
    if (!PyArg_ParseTuple(args, "i", &word))
        return NULL;
    page_deserialize(word);
    return Py_BuildValue ( "(iiii)",
        vec_article[word].table,
        vec_article[word].len,
        vec_float[word].table,
        1
     );
}

static PyObject* _free(PyObject* self, PyObject* args)
{
    int l1, len, l2, freeable;
    if (!PyArg_ParseTuple(args, "(iiii)", &l1, &len, &l2, &freeable))
        return NULL;
   
    if( !freeable )
    {
        free( (int*) l1);
        free( (float*) l2);
    }
   return Py_BuildValue("i",  0 ) ;     
}


static PyObject* _loadNode(PyObject* self, PyObject* args)
{
    char* word;
    if (!PyArg_ParseTuple(args, "s", &word))
        return NULL;
        
   load(word);
   return Py_BuildValue("i",  0 ) ; 
}


static PyMethodDef HelloMethods[] =
{
     {"put", put, METH_VARARGS, "Puts value into [intialized] index."},
     {"indeks_start", indeks_start, METH_VARARGS, "Initializes index."},          
     {"mock_put", mock_put, METH_VARARGS, "Mocks putting value into [unintialized] index."},
     {"start_structure", start_structure, METH_VARARGS, "Initializes index."},          
     {"getArticles", getArticles, METH_VARARGS, ""},          
     {"getFloats", getFloats, METH_VARARGS, ""},
     {"free",_free, METH_VARARGS, ""},
     {"get", indexGet, METH_VARARGS, "" },
     {"indeks_serialize", indeks_serialize, METH_VARARGS, ""},          
     {"indeks_deserialize", indeks_deserialize, METH_VARARGS, ""},                                   
     {"fully_serialize", fully_indeks_serialize, METH_VARARGS, ""},           
     {"loadIndex", fully_indeks_deserialize, METH_VARARGS, ""},                                   
     {"getVal", extern_getVal, METH_VARARGS, ""},
     {"loadNode", _loadNode, METH_VARARGS, ""},
     {NULL, NULL, 0, NULL}
};
 
PyMODINIT_FUNC initcreateIndex(void)
{
      PyObject* m = Py_InitModule("createIndex", HelloMethods);
     // Py_INCREF(&indeksEntryTypes);
      //PyModule_AddObject(m, "indeksEntry", (PyObject *)&indeksEntryTypes);
}
